package practise.mulitThreads.EventSystem.impl;

import practise.mulitThreads.EventSystem.EventContainer;
import practise.mulitThreads.EventSystem.Server;
import practise.mulitThreads.EventSystem.entity.Event;

import java.util.Date;

/**
 * Created by angela on 7/27/2014.
 */
public class ServerImpl implements Server{
    private static int serverNumner =0;
    private int id;
    private EventContainer eventContainer;


    public ServerImpl(){
        serverNumner++;
        id=serverNumner;
    }

    @Override
    public Event generate() {
        return generate(id);
    }

    private Event generate(int id){
        return new Event(new Date().getTime(),"generate a message in server "+id);
    }

    @Override
    public void register(EventContainer container) {
        this.eventContainer = container;
    }

    @Override
    public void run() {
        try {
            while(!Thread.currentThread().isInterrupted()) {
                Thread.sleep(1000);
                eventContainer.offer(generate());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
